package com.esun.gateway.controller;

import com.esun.gateway.bean.Param;
import com.esun.gateway.bean.Result;
import com.esun.gateway.service.GatewayService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.SQLException;

@RequestMapping(value = "/gateway")
@RestController
@CrossOrigin
public class GatewayAction {

    @Resource
    GatewayService gatewayService;

    @RequestMapping(value = "/routing")
    public Result routing(@RequestBody Param param) throws SQLException {
        Result result = new Result();
        Integer routerType = null ;
        if(param != null && param.getRouterId() != null){
            try {
                routerType = gatewayService.checkRouterType(param.getRouterId());
            } catch (SQLException e) {
                e.printStackTrace();
                result.setSuccess(false);
                result.setMessage("该接口不存在");
                return result;
            }
            switch(routerType){
                case 1:
//                    return gatewayService.add(param);
                case 2:
//                    return gatewayService.delete(param);
                case 3:
                    return gatewayService.update(param);
                case 4:
                    return gatewayService.query(param);
                default:
                    result.setSuccess(false);
                    result.setMessage("接口定义错误,请查看接口类型");
                    return result;
            }
        }else{
            result.setSuccess(false);
            result.setMessage("routerId不能为空");
            return result;
        }

    }
}